/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hoja7;

/**
 *
 * @author luisorozco
 */

import java.util.Scanner;


public class Hoja7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        
        Scanner op = new Scanner(System.in);
        Heap lista;
        Nodo coso;
        String cadena;
        System.out.println("+--------------------------------------+");
        System.out.println("+------ Bienevenido a la Hoja #7 ------+");
        System.out.println("+--------------------------------------+");
        System.out.println("");
        System.out.println("Ingrese la cadena a evaluar:");
        cadena = op.nextLine();
        lista = new Heap(cadena.length());
        System.out.println("");
        System.out.println("+---  El resultado de la cadena es  ---+");
        System.out.println("");
        System.out.println("Caracter |  Frecuencia | Codigo");
        System.out.println("-------------------------------");
   
        for(int i =0; i < cadena.length(); i++) {            
            Nodo tree = new Nodo(cadena.charAt(i));
            coso = lista.remove(tree);
            if (coso == null) {
                lista.insert(tree);

           
            } else {
                coso.incRepet();
                lista.insert(coso);

            }


        }

        while(lista.getAmount() > 1) {
            Nodo ptree = new Nodo();
            int lad1, lad2;
            lad1= lista.seeFirst().getRepet() ;
            ptree.setLeft(lista.remove());
            lad2 = lista.seeFirst().getRepet(); 
            ptree.setRigth(lista.remove());
            ptree.setRepet(lad1+lad2);           
            lista.insert(ptree);
        }
      
       
        
        
        lista.seeFirst().route();


    }
 
    }
